//
// Created by chenchao on 2021/8/12.
//

#include "CCOpenGLBuffer.h"

CCOpenGLBuffer::CCOpenGLBuffer(CCOpenGLBuffer::Type type,CCOpenGLBuffer::UsagePattern usage)
{
    m_buffSize = 0;
    m_usage = usage;
    m_bufferType = type;
}

CCOpenGLBuffer::~CCOpenGLBuffer()
{
    glDeleteBuffers(1, &m_buffID);
}

void CCOpenGLBuffer::Create()
{
    glGenBuffers(1, &m_buffID);
}

void CCOpenGLBuffer::Bind()
{
    glBindBuffer(m_bufferType, m_buffID);

}

void CCOpenGLBuffer::Release()
{
    glBindBuffer(m_bufferType, 0);
}

void CCOpenGLBuffer::SetBufferData(const GLvoid *data, GLsizeiptr size)
{
    if (size > m_buffSize) {
        m_buffSize = size;
        glBufferData(m_bufferType, size, data, m_usage);
    } else {
        glBufferSubData(m_bufferType, 0, size, data);
    }
}